﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShareMarking.Service;
using System.Configuration;

namespace ShareMarking.SyncActivity
{
    class Program
    {
        static void Main(string[] args)
        {
            string httpstr = ConfigurationManager.AppSettings["vigohttp"];
            string signsecret = ConfigurationManager.AppSettings["signscrect"];
            var actlist = qr_ActivityService.Instance.Get_AllData_List();
            ComLib.LogLib.Log4NetBase.Log("得到活动个数:" + actlist.Count);

            for (int i = 0; i < actlist.Count; i++)
            {
                var act = actlist[i];

                #region 判断需不需要更新
                var currenturl = "";
                if (DateTime.Now < act.A_StartTime)
                {
                    currenturl = act.A_wxbeforeUrl;
                }
                else if (DateTime.Now > act.A_StartTime && DateTime.Now < act.A_EndTime)
                {
                    currenturl = act.A_wxUrl;
                }
                else
                {
                    currenturl = act.A_wxafterUrl;
                }
                
                #endregion
                if (currenturl != act.A_currentUrl)
                {
                    #region 组装消息

                    var ssacode = "";
                    if (currenturl.Contains("?"))
                    {
                        ssacode = "$sacode=" + act.A_SaCode;
                    }
                    else
                    {
                        ssacode = "?sacode=" + act.A_SaCode;
                    }

                    var extra = new List<ShareMarking.Interface.extra>() { 
                        new ShareMarking.Interface.extra { 
                            title = act.A_wxTitle, 
                            shortdesc = act.A_wxDesc, 
                            linkurl = (currenturl+ssacode).Replace('&','$'),
                            picurl ="http://admin.mjn.c2d.me/"+ act.A_wxImgUrl
                        }
                    };
                    var message = new ShareMarking.Interface.message2()
                    {
                        id = act.A_wxQrCodeID,
                        title = act.A_Name,
                        remark = act.A_Name + "的二维码",
                        sacode = "test",
                        extra = extra
                    };
                    #endregion

                    #region vigo接口申请
                    var vigo = new ShareMarking.Interface.Qrcode_vigo(httpstr, signsecret);
                    var resultentity = vigo.UpdateQrcode(message);
                    #endregion

                    #region 保存活动
                    act.DateModified = DateTime.Now;
                    act.A_currentUrl = currenturl;//当前使用的URL
                    var result = qr_ActivityService.Instance.Update_Entity(act);
                    #endregion

                    ComLib.LogLib.Log4NetBase.Log("更新活动地址为:" + act.ActivityID + ":" + act.A_Name + ":" + act.A_currentUrl);
                }
                else
                {
                    ComLib.LogLib.Log4NetBase.Log("活动不需要更新:" + act.ActivityID + ":" + act.A_Name);
                }
            }
        }
    }
}
